#include<iostream>
// #include<algorithm
#include<cstring>
using namespace std;
const int N = 310;
int sum[N];
int f[N][N];
int main()
{
    int n; cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int x; cin >> x;
        sum[i] = sum[i - 1] + x;
    }
    // fill()
    memset(f, 0x3f, sizeof f);
    for (int i = 0; i <= n; i++) f[i][i] = 0;
    // zdl:: 现在要做的就是枚举所有的右端点
    for (int len = 2; len <= n; len++)
    {
        for (int i = 1; i + len - 1 <= n; i++)
        {
            int j = i + len -1;
            int t = sum[j] - sum[i - 1];
            for (int k = i; k < j; k++)
            {
                // zdl::现在就是分为了两个区域 [i , k] [k + 1, j - 1]
                f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j] + t);
            } 
        }
    }

    cout << f[1][n] << endl;
    return 0;
}


// zdl::式子合并的本质就是在区间dp的基础上再枚举边界点